package com.xliang.spider.version02;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.xliang.spider.util.CookiesUtil;
import com.xliang.spider.util.Video;
import com.xliang.spider.util.VideoUtil;

/**
 * 扫描所有职业课程的代码
 * @author xiongl
 *
 */
public class SpiderScanVideo extends Thread
{
    private String url;
    
    public SpiderScanVideo(String url)
    {
        this.url = url;
    }
    
    @Override
    public void run()
    {
        try
        {
            scanVideo(url);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    private org.jsoup.Connection getConnection(String url)
    {
        org.jsoup.Connection con = Jsoup.connect(url);
        con.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        con.header("Accept-Encoding", "gzip, deflate");
        con.header("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
        con.header("Connection", "keep-alive");
        con.header("Host", "www.jikexueyuan.com");
        con.header("Referer", url);
        con.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0");
        con.timeout(60000);
        return con;
    }
    
    public void scanVideo(String url)
        throws Exception
    {
        
        Document rootDocument = getConnection(url).get();
        Elements rootElements = rootDocument.getElementsByClass("interleave-odd").select("td > a");
        int index = 1;
        for (Element parent : rootElements)
        {
            String parentHref = parent.attr("href");
            String parentTitle =
                parent.attr("title")
                    .replaceAll("：", "")
                    .replaceAll(":", "")
                    .replaceAll("“", "")
                    .replaceAll("”", "")
                    .replaceAll("’", "")
                    .replaceAll("。", "")
                    .replaceAll("\"", "")
                    .replaceAll("/", "、");
            
            System.out.println(String.format("正在扫描%s的所有视频", parentTitle));
            
            try
            {
                Document childDocument = getConnection(parentHref).get();
                Elements childElements = childDocument.getElementsByClass("text-box").select("h2 > a");
                for (Element child : childElements)
                {
                    String href = child.attr("href");
                    String title =
                        child.text()
                            .replaceAll("：", "")
                            .replaceAll(":", "")
                            .replaceAll("“", "")
                            .replaceAll("”", "")
                            .replaceAll("’", "")
                            .replaceAll("。", "")
                            .replaceAll("\"", "")
                            .replaceAll("/", "、");
                    String videoUrl = "";
                    String videoName = String.format("%s-%s.mp4", parentTitle, title);
                    if (href.startsWith("javascript"))
                    {
                        videoUrl = href;
                    }
                    else
                    {
                        try
                        {
                            Document document = getConnection(href).cookies(CookiesUtil.createJKCookies()).get();
                            Element video = document.getElementsByTag("source").first();
                            videoUrl = video.attr("src");
                            System.out.println(String.format("扫描到视频：%s的地址为：%s", videoName, videoUrl));
                            if (videoUrl.startsWith("javascript"))
                            {
                                videoUrl = href;
                            }
                        }
                        catch (Exception e)
                        {
                            videoUrl = href;
                            System.err.println(String.format("扫描到视频：%s的地址%s失败，失败原因：%s", videoName, videoUrl, e.getMessage()));
                        }
                    }
                    addVideo(index, videoName, videoUrl);
                    index++;
                }
            }
            catch (Exception e)
            {
                index++;
                System.err.println(String.format("扫描%s的所有视频发生异常", parentTitle));
            }
        }
    }
    
    public void addVideo(int index, String videoName, String videoUrl)
    {
        Video video = new Video();
        video.setIndex(index);
        video.setName(videoName);
        video.setUrl(videoUrl);
        VideoUtil.addVideo(video);
    }
    
    public static void main(String[] args)
        throws Exception
    {
        Document childDocument = Jsoup.connect("http://www.jikexueyuan.com/course/144.html").timeout(10000).get();
        Elements childElements = childDocument.getElementsByClass("text-box").select("h2 > a");
        System.out.println(childElements);
        
        /*Document rootDocument = Jsoup.connect("http://ke.jikexueyuan.com/zhiye/android/").timeout(10000).get();
        Elements rootElements = rootDocument.getElementsByClass("interleave-odd").select("td > a");
        System.out.println(rootElements);*/
    }
}
